package oracle_sql_option

import (
	"log"
	"sync"
)

var TIME_COUNT sync.WaitGroup

func Load(driver, constr string) error {
	db, err := OpenDb(driver, constr)
	if err != nil {
		return err
	}
	defer db.Close()

	if err := Oracle_Option_FR(FR_SQL, db); err != nil {
		// log.Println(err)
		return err
	}
	return nil
}

var TOTAL_NUM = 0

// Load_Chan 传入db连接条件
func Load_Chan(driver, constr string) error {
	defer func() {
		if err := recover(); err != nil {
			log.Println("channel have err====：", err)
		}
	}()
	db, err := OpenDb(driver, constr)
	if err != nil {
		return err
	}

	num_chan := make(chan int)
	for i := 0; i < 4; i++ {
		TIME_COUNT.Add(1)
		go func(num int) {
			sqlWhere := ""
			switch {
			case num == 0:
				// sqlWhere = "where regexp_like(流水号,'^[0-5]')"
				sqlWhere = "where 流水号 ~ '[^0-5]'"
			case num == 1:
				// sqlWhere = "where regexp_like(流水号,'^[6-9]')"
				sqlWhere = "where 流水号 ~ '[^6-9]'"

			case num == 2:
				// sqlWhere = "where regexp_like(流水号,'^[A-C]')"
				sqlWhere = "where 流水号 ~ '[^A-C]'"

			default:
				// sqlWhere = "where regexp_like(流水号,'^[D-L]')"
				sqlWhere = "where 流水号 ~ '[^D-L]'"
			}
			if err := Oracle_Option_FR_Chan(FR_SQL_CHAN, db, sqlWhere, num_chan); err != nil {
				panic(err)
			}
		}(i)
	}
	for i := 0; i < 4; i++ {
		TOTAL_NUM += <-num_chan
	}
	TIME_COUNT.Wait()
	db.Close()
	log.Println("总共执行数据量为===：", TOTAL_NUM)
	return nil
}
